<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PersonLinkPerson
 *
 * @author jbecker
 */
class Model_PersonLinkPerson extends Model_Base
{

    protected $name = 'person_link_person';

    public function create(array $personLinkPerson)
    {
	$this->db->insert($this->name, $personLinkPerson);
	return $this->db->lastInsertId($this->name);
    }
     public function find($id)
    {
	$select = $this->db->select()
		->from($this->name)
		->where('id = ?', $id);
	return $this->db->fetchRow($select);
    }
    public function findByPerson($idperson)
    {
	$select = $this->db->select()
		->from($this->name)
		->where('idperson1 = ?', $idperson)
		->orWhere('idperson2 = ?', $idperson);
	return $this->db->fetchRow($select);
    }
    public function findParents($idchild)
    {
	$sql =	'SELECT 
		    *,
		    plp.id AS plp_id,
		    p.id AS id
	       FROM 
		    `person` AS `p`,
		    `person_link_person` AS `plp`
	       WHERE
		    (plp.idperson1=p.id OR plp.idperson2=p.id)
	       AND
		    (plp.idperson1=? OR plp.idperson2=?)
	       AND
		    p.person_type=?';
	return $this->db->fetchAll($sql,array($idchild,$idchild,'Elternteil'));
    }
    public function findChildren($idparent)
    {
	$sql =	'SELECT 
		    *,
		    plp.id AS plp_id,
		    p.id AS id
	       FROM 
		    `person` AS `p`,
		    `person_link_person` AS `plp`
	       WHERE
		    (plp.idperson1=p.id OR plp.idperson2=p.id)
	       AND
		    (plp.idperson1=? OR plp.idperson2=?)
	       AND
		    p.person_type=?';
	return $this->db->fetchAll($sql,array($idparent,$idparent,'Kind'));
    }
}

?>
